package com.example.administrator.waitan.find.art.ArtContentActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;

import java.util.List;

/**
 * Created by Administrator on 2016/1/16.
 */
public class WomanContentBaseAdapter extends BaseAdapter {
    private List<WomanContentInfo> data;
    private Context context;
    private RequestQueue queue;
    private ImageLoader imageLoader;
    private ImageLoader.ImageCache imageCache;
    private LruCache<String,Bitmap> lruCache;
    public WomanContentBaseAdapter(List<WomanContentInfo> data, Context context) {
        this.data = data;
        this.context = context;
        init();
    }

    private void init() {
        queue = Volley.newRequestQueue(context);
        lruCache = new LruCache<String,Bitmap>(20);
        imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                lruCache.put(url,bitmap);
            }
        };
        imageLoader = new ImageLoader(queue,imageCache);
    }


    public void getBitmapCache(String imgUrl, ImageView imageView){
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.mi_icon,R.mipmap.mi_icon);
        imageLoader.get(imgUrl,listener,0,0);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyViewHolder myViewHolder;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_lv_art_woman_content,null);
            myViewHolder = new MyViewHolder();
            myViewHolder.content = (TextView) view.findViewById(R.id.tv_item_women_content_content);
            myViewHolder.pic = (ImageView) view.findViewById(R.id.iv_item_women_content_pic);
            view.setTag(myViewHolder);
        }
        else {
            myViewHolder = (MyViewHolder) view.getTag();
        }
        myViewHolder.content.setText(data.get(i).getContent());
        String imgUrl = data.get(i).getBigUrl();
        getBitmapCache(imgUrl,myViewHolder.pic);
        return view;
    }
    class MyViewHolder{
        TextView content;
        ImageView pic;
    }

}
